//index.js
//获取应用实例
import http from '../../../class/Utils/Http.js'
import user from '../../../class/Utils/User.js'
import tips from '../../../class/Utils/Tips.js'

const app = getApp()


Page({
    data: {
      nolist:0
    },

    onLoad: function () {
     
    

    },
    onShow: function () {
        var that = this;
      tips.loading()

    

      //加入的专业圈
      var that = this
      var user = wx.getStorageSync('user');
      if (user) {
        http.req("/Api/quan_list", { uid: user.userid }, function (res) {
          that.setData({
            myqlist: res.list,
            myqcnt: res.count
          })


          http.req('/syq/index', {}, function (res) {
            var myqcnt = that.data.myqcnt
            var myqlist = that.data.myqlist
            if (res.q_info) {
              for (var i = 0; i < res.q_info.length; i++) {
                res.q_info[i].changduans = 1;

                //判断会员是否加入该圈
                res.q_info[i].is_join = 0
                if (myqcnt) {
                  for (var j = 0; j < myqcnt; j++) {
                    if (myqlist[j].url == res.q_info[i].url) {
                      res.q_info[i].is_join = 1
                    }
                  }
                }
              }
              that.setData({
                quan: res.q_info
              })
            } else {
              that.setData({
                nolist:1
              })
            }
          }); 




        })
      } else {
        that.setData({
          myqcnt: 0
        })
        http.req('/syq/index', {}, function (res) {
         
          if (res.q_info.length>0) {
            for (var i = 0; i < res.q_info.length; i++) {
              res.q_info[i].changduans = 1;

              res.q_info[i].is_join = 0
             
            }
            that.setData({
              quan: res.q_info
            })
          } else {
           that.setData({
             nolist:1
           })
          }
        }); 



      }





      
      


      tips.loaded()
      
    },

    //搜索跳转页面
    searchlink: function (e) {
        var that = this
        wx.navigateTo({
            url: '/pages/shengyiquan/search/search'
        })
    },
    //长短 介绍切换
    changeintro: function (e) {
        var that = this
        console.log(e)
        var presentindex = e.currentTarget.dataset.index
        var datalist = that.data.quan
        var datalistzhi = datalist[presentindex].changduans

        for (var i = 0; i < datalist.length; i++) {

            if (datalistzhi == 1) {
                datalist[presentindex].changduans = 0

            }
            else {
                datalist[presentindex].changduans = 1
            }
        }


        that.setData({
            quan: datalist
        })
        console.log(datalist)


    },
    //监听 page 滚动条位置
    onPageScroll: function (res) {
        var that = this;
        var topscroll = res.scrollTop;
       // console.log(res.scrollTop);
        if (topscroll > 50) {
            that.setData({
                searchtig: true,
            })
        } else {
            that.setData({
                searchtig: false,
            })
        }

    },

    /*
    加入生意圈
     */
    jiaru:function (e) {
        var that = this
        user.log_user(that, function (ures) {
            var id = e.currentTarget.dataset.id
            var name = e.currentTarget.dataset.name
          var index = e.currentTarget.dataset.index
            tips.modal("是否加入 '"+name+"'",true,'function',false,function () {

                var data = {
                    uid: ures.userid,
                    qid: id
                }
                console.log(data)
                http.req("/Api/add_quan", data, function (res) {
                    console.log(res)
                    if (res.code == '204') {
                        tips.ok(res.msg_val)


                     
                      var datalist = that.data.quan
                      datalist[index].is_join=1
                       
                      that.setData({
                        quan: datalist
                      })

                      http.req("/Api/quan_list", { uid: ures.userid }, function (res) {
                        that.setData({
                          myqlist: res.list,
                          myqcnt: res.count
                        })
                      })



                      //tips.closeall('/pages/shengyiquan/list/list')


                    } else {
                        tips.error(res.msg_val)
                    }
                })
            })

        })
    },
    /*
     授权登录
     */
    bindGetUserInfo: function (e) {
        var that = this
        user.user(this, function (res) {
            that.setData({
                shouquan: 0,
                userinfo: res

            })
            if (res.log == 0) {
                that.setData({
                    bangdingtoocletag: true
                })
            }
        })

    },
    /*
     弹出 登录
     */
    bangdingtoocle: function () {
        var that = this
        that.setData({
            bangdingtoocletag: !that.data.bangdingtoocletag
        })
    },
    /*
     点击登录
     */
    denglu: function (e) {
        var that = this
        var zhanghao = e.detail.value.zhanghao
        var mima = e.detail.value.mima
        if (!zhanghao) {
            tips.error("账号不能为空");
            return false
        }
        if (!mima) {
            tips.error("密码不能为空");
        }
        http.req("/Api/log", {login: zhanghao, passwd: mima, uid: wx.getStorageSync('user').userid}, function (res) {
            console.log(res)
            if (res.code == '200') {
                var user = wx.getStorageSync('user')
                user.log = 1
                wx.setStorageSync('user', user)
                tips.ok('登录成功')
                that.setData({
                    bangdingtoocletag: false,
                    shengyihao: zhanghao
                })
            } else {
                tips.error(res.msg_val)
            }
        })
    },
  //获取绑定微信的手机号码
  getPhoneNumber: function (e) {
    var that = this
    user.getPhoneNumber(e, that)
  },

})
